Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm] Add extending conversions to PackedSimd #86252

Merged

Conversation

radekdoulik
Copy link
Member

No description provided.

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost assigned radekdoulik May 15, 2023
@radekdoulik radekdoulik requested a review from lewing May 15, 2023 15:09
@ghost
Copy link

ghost commented May 15, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime-intrinsics
See info in area-owners.md if you want to be subscribed.

Issue Details

null

Author: radekdoulik
Assignees: -
Labels:

area-System.Runtime.Intrinsics, new-api-needs-documentation

Milestone: -

@radekdoulik
Copy link
Member Author

Test method:

    static void TestExtConversions()
    {
        Vector128<int> v1int = Vector128.Create(RandomInt, RandomInt, RandomInt, RandomInt);
        Vector128<uint> v1uint = Vector128.Create((uint)Random.Shared.Next(), (uint)Random.Shared.Next(), (uint)Random.Shared.Next(), (uint)Random.Shared.Next());
        Vector128<byte> v1byte = Vector128.Create(0, (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next(), (byte)Random.Shared.Next());
        Vector128<sbyte> v1sbyte = Vector128.Create(RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte, RandomSByte);
        Vector128<short> v1short = Vector128.Create(RandomShort, RandomShort, RandomShort, RandomShort, RandomShort, RandomShort, RandomShort, RandomShort);
        Vector128<ushort> v1ushort = Vector128.Create((ushort)Random.Shared.Next(), (ushort)Random.Shared.Next(), (ushort)Random.Shared.Next(), (ushort)Random.Shared.Next(), (ushort)Random.Shared.Next(), (ushort)Random.Shared.Next(), (ushort)Random.Shared.Next(), (ushort)Random.Shared.Next());

        Console.WriteLine($"v1sbyte: {v1sbyte}");
        Console.WriteLine($"v1byte: {v1byte}");
        Console.WriteLine($"v1short: {v1short}");
        Console.WriteLine($"v1ushort: {v1ushort}");
        Console.WriteLine($"v1int: {v1int}");
        Console.WriteLine($"v1uint: {v1uint}");

        Console.WriteLine($"sbyte to short sign ext (lower): {PackedSimd.SignExtendWideningLower(v1sbyte)}");
        Console.WriteLine($"sbyte to short sign ext (upper): {PackedSimd.SignExtendWideningUpper(v1sbyte)}");
        Console.WriteLine($"sbyte to short zero ext (lower): {PackedSimd.ZeroExtendWideningLower(v1sbyte)}");
        Console.WriteLine($"sbyte to short zero ext (upper): {PackedSimd.ZeroExtendWideningUpper(v1sbyte)}");

        Console.WriteLine($"byte to ushort sign ext (lower): {PackedSimd.SignExtendWideningLower(v1byte)}");
        Console.WriteLine($"byte to ushort sign ext (upper): {PackedSimd.SignExtendWideningUpper(v1byte)}");
        Console.WriteLine($"byte to ushort zero ext (lower): {PackedSimd.ZeroExtendWideningLower(v1byte)}");
        Console.WriteLine($"byte to ushort zero ext (upper): {PackedSimd.ZeroExtendWideningUpper(v1byte)}");

        Console.WriteLine($"short to int sign ext (lower): {PackedSimd.SignExtendWideningLower(v1short)}");
        Console.WriteLine($"short to int sign ext (upper): {PackedSimd.SignExtendWideningUpper(v1short)}");
        Console.WriteLine($"short to int zero ext (lower): {PackedSimd.ZeroExtendWideningLower(v1short)}");
        Console.WriteLine($"short to int zero ext (upper): {PackedSimd.ZeroExtendWideningUpper(v1short)}");

        Console.WriteLine($"ushort to uint sign ext (lower): {PackedSimd.SignExtendWideningLower(v1ushort)}");
        Console.WriteLine($"ushort to uint sign ext (upper): {PackedSimd.SignExtendWideningUpper(v1ushort)}");
        Console.WriteLine($"ushort to uint zero ext (lower): {PackedSimd.ZeroExtendWideningLower(v1ushort)}");
        Console.WriteLine($"ushort to uint zero ext (upper): {PackedSimd.ZeroExtendWideningUpper(v1ushort)}");

        Console.WriteLine($"int to long sign ext (lower): {PackedSimd.SignExtendWideningLower(v1int)}");
        Console.WriteLine($"int to long sign ext (upper): {PackedSimd.SignExtendWideningUpper(v1int)}");
        Console.WriteLine($"int to long zero ext (lower): {PackedSimd.ZeroExtendWideningLower(v1int)}");
        Console.WriteLine($"int to long zero ext (upper): {PackedSimd.ZeroExtendWideningUpper(v1int)}");

        Console.WriteLine($"uint to ulong sign ext (lower): {PackedSimd.SignExtendWideningLower(v1uint)}");
        Console.WriteLine($"uint to ulong sign ext (upper): {PackedSimd.SignExtendWideningUpper(v1uint)}");
        Console.WriteLine($"uint to ulong zero ext (lower): {PackedSimd.ZeroExtendWideningLower(v1uint)}");
        Console.WriteLine($"uint to ulong zero ext (upper): {PackedSimd.ZeroExtendWideningUpper(v1uint)}");
    }

output:

  v1sbyte: <-2, -25, 81, 85, 94, -114, -46, 18, 1, -66, 46, -73, 76, -4, 124, 88>
  v1byte: <0, 229, 13, 234, 228, 107, 238, 119, 4, 190, 183, 84, 87, 127, 46, 135>
  v1short: <-21942, 12726, -31745, -3459, 11206, -25452, 15233, -19319>
  v1ushort: <58688, 38751, 4831, 63651, 29450, 49653, 62398, 19778>
  v1int: <-330723718, 654137123, 666254210, -764101589>
  v1uint: <1734002338, 80703557, 1326853359, 991892688>
  sbyte to short sign ext (lower): <-2, -25, 81, 85, 94, -114, -46, 18>
  sbyte to short sign ext (upper): <1, -66, 46, -73, 76, -4, 124, 88>
  sbyte to short zero ext (lower): <254, 231, 81, 85, 94, 142, 210, 18>
  sbyte to short zero ext (upper): <1, 190, 46, 183, 76, 252, 124, 88>
  byte to ushort sign ext (lower): <0, 65509, 13, 65514, 65508, 107, 65518, 119>
  byte to ushort sign ext (upper): <4, 65470, 65463, 84, 87, 127, 46, 65415>
  byte to ushort zero ext (lower): <0, 229, 13, 234, 228, 107, 238, 119>
  byte to ushort zero ext (upper): <4, 190, 183, 84, 87, 127, 46, 135>
  short to int sign ext (lower): <-21942, 12726, -31745, -3459>
  short to int sign ext (upper): <11206, -25452, 15233, -19319>
  short to int zero ext (lower): <43594, 12726, 33791, 62077>
  short to int zero ext (upper): <11206, 40084, 15233, 46217>
  ushort to uint sign ext (lower): <4294960448, 4294940511, 4831, 4294965411>
  ushort to uint sign ext (upper): <29450, 4294951413, 4294964158, 19778>
  ushort to uint zero ext (lower): <58688, 38751, 4831, 63651>
  ushort to uint zero ext (upper): <29450, 49653, 62398, 19778>
  int to long sign ext (lower): <-330723718, 654137123>
  int to long sign ext (upper): <666254210, -764101589>
  int to long zero ext (lower): <3964243578, 654137123>
  int to long zero ext (upper): <666254210, 3530865707>
  uint to ulong sign ext (lower): <1734002338, 80703557>
  uint to ulong sign ext (upper): <1326853359, 991892688>
  uint to ulong zero ext (lower): <1734002338, 80703557>
  uint to ulong zero ext (upper): <1326853359, 991892688>

emitted code check:

> wa-info -v -d -f TestExtConv src/mono/sample/wasm/console-node/bin/Release/AppBundle/dotnet.native.wasm|grep SIMD|egrep 'ext'
      i16x8.extend.low.i8x16.s    [SIMD]
      i16x8.extend.high.i8x16.s    [SIMD]
      i16x8.extend.low.i8x16.u    [SIMD]
      i16x8.extend.low.i8x16.u    [SIMD]
      i16x8.extend.low.i8x16.s    [SIMD]
      i16x8.extend.high.i8x16.s    [SIMD]
      i16x8.extend.low.i8x16.u    [SIMD]
      i16x8.extend.low.i8x16.u    [SIMD]
      i32x4.extend.low.i16x8.s    [SIMD]
      i32x4.extend.high.i16x8.s    [SIMD]
      i32x4.extend.low.i16x8.u    [SIMD]
      i32x4.extend.low.i16x8.u    [SIMD]
      i32x4.extend.low.i16x8.s    [SIMD]
      i32x4.extend.high.i16x8.s    [SIMD]
      i32x4.extend.low.i16x8.u    [SIMD]
      i32x4.extend.low.i16x8.u    [SIMD]
      i64x2.extend.low.i32x4.s    [SIMD]
      i64x2.extend.high.i32x4.s    [SIMD]
      i64x2.extend.low.i32x4.u    [SIMD]
      i64x2.extend.low.i32x4.u    [SIMD]
      i64x2.extend.low.i32x4.s    [SIMD]
      i64x2.extend.high.i32x4.s    [SIMD]
      i64x2.extend.low.i32x4.u    [SIMD]
 i64x2.extend.low.i32x4.u    [SIMD]

@radekdoulik
Copy link
Member Author

Looks like the SN_ZeroExtendWideningUpper is either not emitted right or reported wrong by wa-info, I will look into that.

@radekdoulik
Copy link
Member Author

Looks like the SN_ZeroExtendWideningUpper is either not emitted right or reported wrong by wa-info, I will look into that.

Looks like llvm is optimizing the code to use i16x8.extend.low.i8x16.u instead of i16x8.extend.high.i8x16.u. Not sure why. The actual output is right though.

The "optimized" code:

...
 i8x16.shuffle 0x00000000000000000f0e0d0c0b0a0908    [SIMD]
 i16x8.extend.low.i8x16.u    [SIMD]
...

@radekdoulik radekdoulik merged commit c887bc7 into dotnet:main May 15, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jun 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants